home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 113_01 / a15get.c < prev    next >
Text File  |  1985-03-09  |  9KB  |  325 lines

  1. /*
  2.     HEADER:        CUG113;
  3.     TITLE:        1802 Cross-Assembler (BDS C Version);
  4.     FILENAME:    A15GET.C;
  5.     VERSION:    1.2;
  6.     DATE:        07/22/1985;
  7.  
  8.     DESCRIPTION:    "This program lets you use your CP/M-80-based computer
  9.             to assemble code for the RCA 1802, 1804, 1805, 1805A,
  10.             1806, AND 1806A microprocessors.  The program is
  11.             written in BDS C for the best possible performance on
  12.             8-bit machines.  All assembler features are supported
  13.             except relocation, linkage, listing control, and
  14.             macros.";
  15.  
  16.     KEYWORDS:    Software Development, Assemblers, Cross-Assemblers,
  17.             RCA, CDP1802, CDP1805A;
  18.  
  19.     SEE-ALSO:    CUG149, 1805A Cross-Assembler (Portable);
  20.  
  21.     SYSTEM:        CP/M-80;
  22.     COMPILERS:    BDS C;
  23.  
  24.     WARNINGS:    "This package is specifically tailored to CP/M-80
  25.             machines and the rather non-standard, but high-
  26.             performance BDS C compiler.  For other environments,
  27.             use the portable version of this package on CUG149.";
  28.  
  29.     AUTHORS:    William C. Colley III;
  30. */
  31.  
  32. /*
  33.     1805A Cross-Assembler  v 1.2
  34.  
  35.     Copyright (c) 1980, 82, 83, 85 William C. Colley, III.
  36.  
  37.     July 1982 -- Adapted from my 1802 cross-assembler.  WCC3.
  38.  
  39.     Vers 1.0 -- March 1983 -- Added 1805A opcodes to the 1805 set.  WCC3.
  40.  
  41.     Vers 1.1 -- March 1983 -- Added CPU pseudo-op to combine 1802 and 1805A
  42.             cross-assemblers into a single program.  WCC3.
  43.  
  44.     Vers 1.2 -- June 1985 -- Fixed IF block nesting mechanism bug and bug
  45.             in 1805A SCAL opcode.  WCC3.
  46.  
  47. File:    a15get.c
  48.  
  49. Routines to get source text from the disk and return it in
  50. manageable chunks such as operators, labels, opcodes, etc.
  51. */
  52.  
  53. /*  Get Globals:  */
  54.  
  55. #include "a15.h"
  56.  
  57. /*
  58. Function to get an opcode from the present source line and return its value
  59. and attributes.  The function returns -1 if no opcode was on the line, 0 if
  60. the opcode was not in the opcode table, and 1 otherwise.  If the opcode was
  61. on the line, but not in the table, the error is marked up.
  62. */
  63.  
  64. getopcod(value,attrib)
  65. char *value, *attrib;
  66. {
  67.     char temp[SYMLEN+1], getopc(), getchr();
  68.  
  69.     switch (getchr(value,SKIP)) {
  70.     case ALPHA:    backchr;  getname(temp);
  71.             if (bsearch(temp,value,attrib,0,numopcs(),getopc) &&
  72.                 (extend || (*attrib & (PSOP | XOPC)) != XOPC))
  73.                 return 1;
  74.             markerr('O');  return 0;
  75.  
  76.     default:    markerr('S');  return -1;
  77.  
  78.     case END_LIN:    return -1;
  79.     }
  80. }
  81.  
  82. /*
  83. Function to look up an item in a fixed table by binary search.  The function
  84. returns 1 if the item was found, 0 if not.  name points to the name to be
  85. looked up (null terminated).  value and attrib catch two bytes of parameters
  86. attached to the item if the item is found.  high and low are for the benefit of
  87. the recursion.  For the first call use the number of items in the table for
  88. high and 0 for low.  table is a pointer to a function to get an item from the
  89. appropriate table.  Note that in the comparison, lower case is converted to
  90. upper case.
  91. */
  92.  
  93. bsearch(name,value,attrib,low,high,table)
  94. char *name, *value, *attrib, (*table)();
  95. int low, high;
  96. {
  97.     char temp[5], *pntr1, *pntr2;
  98.     int t, check;
  99.  
  100.     if (low > high) return 0;
  101.     check = low + (high - low >> 1);  (*table)(check,temp,value,attrib);
  102.     pntr1 = temp;  pntr2 = name;
  103.     while ((t = toupper(*pntr2++) - *pntr1) == 0 && *pntr1++ != '\0');
  104.     if (t) return t > 0 ? bsearch(name,value,attrib,check+1,high,table) :
  105.     bsearch(name,value,attrib,low,check-1,table);
  106.     return 1;
  107. }
  108.  
  109. /*
  110. Function to push back an item so that getitem will retrieve it a second time.
  111. Only one level of pushback is supported.
  112. */
  113.  
  114. backitem(value,attrib)
  115. unsigned value, attrib;
  116. {
  117.     oldvalu = value;  oldattr = attrib;  backflg = TRUE;
  118. }
  119.  
  120. /*
  121. Function to get an item from the present source line.  Items may be
  122. looked-up symbols, numeric constants, string constants, operators, etc.
  123. The function returns the attribute of the item gotten.  value is a
  124. pointer to where the value of the item will be stored, and quoteflg
  125. determines whether getitem will look for one- and two-character string
  126. constants or not.
  127. */
  128.  
  129. getitem(value,quoteflg)
  130. unsigned *value;
  131. char quoteflg;
  132. {
  133.     char c, c1, attrib, *tmpptr, temp[2*SYMLEN];
  134.     unsigned base;
  135.  
  136.     if (backflg) { backflg = FALSE;  *value = oldvalu;  return oldattr; }
  137.     switch ((attrib = getchr(&c,SKIP))) {
  138.     case END_LIN:
  139.     case COMMA:    return attrib;
  140.  
  141.     case NUMERIC:    backchr;  getnum(value,&base);  return VALUE;
  142.  
  143.     case OPERATR:    switch (c) {
  144.                 case '>':
  145.                 case '=':
  146.                 case '<':    getchr(&c1,NOSKIP);
  147.                     if (c == '>' && c1 == '=') c = GRTEQ;
  148.                     else if (c == '>' && c1 == '<') c = NOTEQ;
  149.                     else if (c == '=' && c1 == '>') c = GRTEQ;
  150.                     else if (c == '=' && c1 == '<') c = LESEQ;
  151.                     else if (c == '<' && c1 == '>') c = GRTEQ;
  152.                     else if (c == '<' && c1 == '=') c = LESEQ;
  153.                     else backchr;
  154.  
  155.                 default:    *value = c;  return attrib;
  156.             }
  157.  
  158.     case QUOTE:    if (quoteflg) { *value = c;  return attrib; }
  159.             tmpptr = temp;  attrib = 0;
  160.             while ((getchr(&c1,NOSKIP),c1) != '\n' &&  c1 != c)
  161.                 if (++attrib <= 2) *tmpptr++ = c1;
  162.             if (c1 == '\n' || attrib > 2) {
  163.                 if (c1 == '\n') backchr;
  164.                 evalerr = TRUE;  markerr('"');  return VALUE;
  165.             }
  166.             tmpptr = temp;
  167.             for (*value = 0; attrib != 0; attrib--)
  168.                 *value = (*value << 8) + *tmpptr++;
  169.             return VALUE;
  170.  
  171.     case ALPHA:    backchr;  getname(temp);
  172.             if (strcmp(temp,"$") == 0) {
  173.                 *value = pc;  return VALUE;
  174.             }
  175.             if ((*value = chkoprat(temp) & 0xff) != NO_OPR)
  176.                 return OPERATR;
  177.             strcat(temp,PADDING);
  178.             if (slookup(temp) == 0) {
  179.                 *value = sympoint -> symvalu;
  180.                 if (sympoint -> symname[0] > 0x7f)
  181.                 directok = FALSE;
  182.             }
  183.             else { evalerr = TRUE;  markerr('U'); }
  184.             return VALUE;
  185.     }
  186. }
  187.  
  188. /*
  189. Function to get a symbol name, opcode, or label from the present source
  190. line.  The name is returned (null terminated) in buffer.  Names are
  191. terminated by any non-alphanumeric character.
  192. */
  193.  
  194. getname(buffer)
  195. char *buffer;
  196. {
  197.     char c, d, count;
  198.  
  199.     count = 0;
  200.     while ((c = getchr(&d,NOSKIP)) == ALPHA || c == NUMERIC)
  201.     if (++count <= SYMLEN) *buffer++ = d;
  202.     backchr;  *buffer = '\0';
  203. }
  204.  
  205. /*
  206. Function to check a string against the list of operators that get spelled out.
  207. This list consists of GE, GT, LE, LT, EQ, NE, AND, OR, XOR, NOT, MOD, SHL,
  208. SHR, HIGH, LOW.  The function returns the token for the operator if the
  209. string is an operator, the token NO_OPR otherwise.
  210. */
  211.  
  212. chkoprat(string)
  213. char *string;
  214. {
  215.     char s, t, getopr();
  216.  
  217.     return (bsearch(string,&s,&t,0,numoprs(),getopr)) ? t : NO_OPR;
  218. }
  219.  
  220. /*
  221. Function to get an Intel format number from the present input line.
  222. value points to the number to be returned, and base points to an
  223. unsigned where the base of the number will be saved during internal
  224. computations.  The function returns a fixed value of 1 and flags errors
  225. as it goes along.
  226. */
  227.  
  228. getnum(number,base)
  229. unsigned *number, *base;
  230. {
  231.     char c1, c2, toupper();
  232.     unsigned b;
  233.  
  234.     if ((c1 = getchr(&c2,NOSKIP)) != ALPHA && c1 != NUMERIC) {
  235.     backchr;  return 0xffff;
  236.     }
  237.     if ((b = getnum(number,base)) == 0xffff) {
  238.     *number = 0;
  239.     switch (toupper(c2)) {
  240.         case 'H':    *base = 16;  return 1;
  241.  
  242.         case 'D':    *base = 10;  return 1;
  243.  
  244.         case 'O':
  245.         case 'Q':    *base = 8;  return 1;
  246.  
  247.         case 'B':    *base = 2;  return 1;
  248.  
  249.         default:    *base = 10;  b = 1;
  250.     }
  251.     }
  252.     *number += b * _aton(toupper(c2),*base);
  253.     return b * *base;
  254. }
  255.  
  256. /*
  257. Internal function in getnum to convert a character to a digit.  The base
  258. is passed so that this routine can check for digits larger than the base.
  259. */
  260.  
  261. _aton(letter,base)
  262. char letter;
  263. unsigned base;
  264. {
  265.     unsigned n;
  266.  
  267.     if (letter >= '0' && letter <= '9') n = letter - '0';
  268.     else if (letter >= 'A' && letter <= 'F') n = letter - ('A' - 10);
  269.     else { markerr('E');  evalerr = TRUE;  return 0; }
  270.     if (n < base) return n;
  271.     markerr('D');  evalerr = TRUE;  return 0;
  272. }
  273.  
  274. /*
  275. Function to get a character from the present source line.  The function
  276. returns the attribute of the character fetched.  value is a pointer to
  277. the location where the character itself will be saved, and skipflg
  278. deter